const url = require('url');
const got = require('@/utils/got');
const cheerio = require('cheerio');

module.exports = async (ctx) => {
    const root_url = 'https://www.cir.cn/Rss/Default.xml';
    const response = await got({
        method: 'get',
        url: root_url,
    });

    const cont = cheerio.load(response.data, {xmlMode:true})
    const list = cont('item');

    const count = [];
    for (let i = 0; i < Math.min(list.length, 20); ++i) {
        count.push(i);
    }

    const items = await Promise.all(
        count.map((i) => {
            const item = list[i];
            const title = cont(item).find('title').text();
            const link = cont(item).find('link').text();
            const pubDate = cont(item).find('pubDate').text();

            if (link.startsWith('https://txt.cir.cn')) {
                return ctx.cache.tryGet('circn_' + link, async () => {
                    const res = await got({ method: 'get',
                        url: link,
                    });
                    const content = cheerio.load(res.data);
                    const post = content('#zzl_content');

                    return {
                        title: title,
                        link: link,
                        pubDate,
                        description: post.html(),
                    };
                });
            } else {
                return null
            }

        })
    );

    ctx.state.data = {
        title: '产业调研网 - 市场调研',
        link: root_url,
        item: items.filter((x) => !!x),
    };
};
